﻿/*
 * @author: wizardc
 */

using Cysharp.Threading.Tasks;

namespace DouEditor.Resource
{
    /// <summary>
    /// 打包步骤基类
    /// </summary>
    public abstract class BuildStepBase
    {
        protected IBuildStepData _buildStepData;
        
        private string _error;
        private bool _executeSuccess;
        private bool _completeFlag;

        /// <summary>
        /// 错误描述
        /// </summary>
        public string error => _error;

        public void Init(IBuildStepData buildStepData)
        {
            _buildStepData = buildStepData;
        }
        
        public async UniTask<bool> Execute()
        {
            Start();
            await UniTask.WaitUntil(() => _completeFlag);
            return _executeSuccess;
        }
        
        /// <summary>
        /// 处理本步骤
        /// </summary>
        protected abstract void Start();

        /// <summary>
        /// 标记当前打包步骤执行失败
        /// </summary>
        protected void Failed(string error)
        {
            _error = error;
            _executeSuccess = false;
            _completeFlag = true;
        }
        
        /// <summary>
        /// 标记当前打包步骤执行完毕
        /// </summary>
        protected void Complete()
        {
            _executeSuccess = true;
            _completeFlag = true;
        }
    }
}
